<template>
  <div class="app-container">
    <div class="button-area">
      <el-button
        type="primary"
        @click="
          dialogFormVisible = true
          isEdit = false
        "
        >新增</el-button
      >
    </div>
    <el-table :data="tableData" style="width: 100%">
      <el-table-column prop="name" label="名称"> </el-table-column>
      <el-table-column prop="typeId" label="类型">
        <template slot-scope="scope">
          <span>{{ getType(scope.row.typeId) }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="tag" label="标签"> </el-table-column>
      <el-table-column prop="bandName" label="品牌"> </el-table-column>
      <el-table-column prop="cas" label="CAS"> </el-table-column>
      <el-table-column prop="price" label="价格"> </el-table-column>
      <el-table-column prop="sku" label="SKU"> </el-table-column>
      <el-table-column prop="unitId" label="单位">
        <template slot-scope="scope">
          <span>{{ getUnit(scope.row.unitId) }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="action" label="操作">
        <template slot-scope="scope">
          <el-button
            @click="handleClick(scope.row, 'delete')"
            type="text"
            size="small"
            >删除</el-button
          >
          <el-button
            type="text"
            size="small"
            @click="handleClick(scope.row, 'edit')"
            >编辑</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination-container">
      <el-pagination
        layout="prev, pager, next"
        :total="total"
        :page-size="pagesize"
        :current-page="page"
      >
      </el-pagination>
    </div>
    <el-dialog title="试剂" :visible.sync="dialogFormVisible" width="80%">
      <el-form :model="form" :rules="rules" ref="form" :inline="true">
        <el-row>
          <el-col :span="8">
            <el-form-item label="名称" required prop="name">
              <el-input v-model="form.name" autocomplete="off"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="别名" prop="aliasName">
              <el-input v-model="form.aliasName" autocomplete="off"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="类型" required prop="typeId">
              <el-select v-model="form.typeId" placeholder="请选择">
                <el-option
                  v-for="item in types"
                  :key="item.id"
                  :label="item.type"
                  :value="item.id"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="品牌" prop="bandName">
              <el-input v-model="form.bandName" autocomplete="off"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="CAS" prop="cas">
              <el-input v-model="form.cas" autocomplete="off"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="单位" prop="unitId">
              <el-select v-model="form.unitId" placeholder="请选择">
                <el-option
                  v-for="item in units"
                  :key="item.id"
                  :label="item.unit"
                  :value="item.id"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8"
            ><el-form-item label="SKU" prop="sku">
              <el-input
                v-model="form.sku"
                autocomplete="off"
              ></el-input> </el-form-item
          ></el-col>
          <el-col :span="8"
            ><el-form-item label="纯度" prop="purity">
              <el-input
                v-model="form.purity"
                autocomplete="off"
              ></el-input> </el-form-item
          ></el-col>
          <el-col :span="8">
            <el-form-item label="价格" prop="price">
              <el-input-number
                v-model="form.price"
                :min="0"
                label="描述文字"
              ></el-input-number>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="过期月份" prop="expressMounth">
              <el-input-number
                v-model="form.expressMounth"
                :min="0"
                label="描述文字"
              ></el-input-number>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="最大库存" prop="maxStock">
              <el-input-number
                v-model="form.maxStock"
                :min="0"
                label="描述文字"
              ></el-input-number>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="最小库存" prop="minStock">
              <el-input-number
                v-model="form.minStock"
                :min="0"
                label="描述文字"
              ></el-input-number>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="标签" prop="tag">
              <el-input v-model="form.tag" autocomplete="off"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" autocomplete="off"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="存储">
              <el-checkbox-group v-model="form.saveType">
                <el-checkbox label="1" name="saveType">没有易燃物</el-checkbox>
                <el-checkbox label="2" name="saveType">没有氧化剂</el-checkbox>
                <el-checkbox label="3" name="saveType">保持冷藏</el-checkbox>
                <el-checkbox label="4" name="saveType">保持冷冻</el-checkbox>
                <el-checkbox label="5" name="saveType">保持干燥</el-checkbox>
                <el-checkbox label="6" name="saveType">保持避光</el-checkbox>
                <el-checkbox label="7" name="saveType">无酸</el-checkbox>
                <el-checkbox label="8" name="saveType">无碱</el-checkbox>
              </el-checkbox-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="使用">
              <el-checkbox-group v-model="form.useType">
                <el-checkbox label="1" name="useType">防护服</el-checkbox>
                <el-checkbox label="2" name="useType">护目镜</el-checkbox>
                <el-checkbox label="3" name="useType">手套</el-checkbox>
                <el-checkbox label="4" name="useType">防毒口罩</el-checkbox>
                <el-checkbox label="5" name="useType">通风橱</el-checkbox>
              </el-checkbox-group>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm('form')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listReagent,
  newReagent,
  delReagent,
  editReagent,
} from '@/api/reagent/product'
import { listReagentType } from '@/api/reagent/productType'
import { listReagentUnit } from '@/api/reagent/unit'
export default {
  name: 'Product',
  data() {
    return {
      tableData: [],
      dialogFormVisible: false,
      form: {
        aliasName: '',
        bandName: '',
        cas: '',
        expressMounth: 12,
        maxStock: 10000,
        minStock: 0,
        name: '',
        price: 0,
        purity: '',
        remark: '',
        saveType: [],
        sku: '',
        tag: '',
        typeId: '',
        useType: [],
        unitId: '',
      },
      rules: {
        name: [{ required: true, message: '请输入名称', trigger: 'blur' }],
        typeId: [{ required: true, message: '请选择类型', trigger: 'change' }],
      },
      types: [],
      units: [],
      isEdit: false,
      editId: '',
      total: 0,
      page: 1,
      pagesize: 10,
    }
  },
  created() {
    const self = this
    self.getReagent()
    self.getReagentType()
    self.getReagentUnit()
  },
  methods: {
    getReagentType() {
      const self = this
      listReagentType().then((response) => {
        if (response && response.rows) {
          self.types = response.rows
        }
      })
    },
    getReagent() {
      const self = this
      listReagent({pageNum: self.page, pageSize: self.pagesize}).then((response) => {
        if (response && response.rows) {
          self.tableData = response.rows;
          self.total = response.total;
        }
      })
    },
    getReagentUnit() {
      const self = this
      listReagentUnit().then((response) => {
        if (response && response.rows) {
          self.units = response.rows
        }
      })
    },
    submitForm(formName) {
      const self = this
      self.$refs[formName].validate((valid) => {
        if (valid) {
          const sendValue = {
            ...self.form,
            saveType: self.form.saveType.toString(),
            useType: self.form.useType.toString(),
          }
          if (self.isEdit) {
            editReagent(sendValue).then((response) => {
              if (response && response.code === 200) {
                self.dialogFormVisible = false
                self.$message({
                  message: '操作成功',
                  type: 'success',
                })
                self.getReagent()
              }
            })
          } else {
            newReagent(sendValue).then((response) => {
              if (response && response.code === 200) {
                self.dialogFormVisible = false
                self.$message({
                  message: '操作成功',
                  type: 'success',
                })
                self.getReagent()
              }
            })
          }
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    handleClick(row, type) {
      const self = this
      if (type === 'delete') {
        self
          .$confirm('此操作将永久删除该试剂, 是否继续?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
          })
          .then(() => {
            delReagent(row.id).then((response) => {
              if (response && response.code === 200) {
                self.$message({
                  message: '操作成功',
                  type: 'success',
                })
                self.getReagent()
              }
            })
          })
      } else if (type === 'edit') {
        self.isEdit = true
        self.editId = row.id
        self.form = {
          ...row,
          saveType: row.saveType.split(','),
          useType: row.useType.split(','),
        }
        self.dialogFormVisible = true
      }
    },
    getType(typeId) {
      const self = this;
      let result = '';
      self.types.map((item) => {
        if (item.id = typeId) {
          result = item.type;
        }
      })
      return result;
    },
    getUnit(unitId) {
      const self = this;
      let result = '';
      self.units.map((item) => {
        if (item.id = unitId) {
          result = item.unit;
        }
      })
      return result;
    },
  },
}
</script>

<style></style>
